package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.BaseUIActivity;
import com.ouertech.android.agm.lib.ui.base.UIOptions;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Tag;
import com.simon.mengkou.data.enums.EImageTagType;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.adapter.SearchImageTagAdapter;
import com.simon.mengkou.ui.adapter.SearchTagAdapter;
import com.simon.mengkou.utils.UtilOuer;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xiazhenshui on 16/6/23.
 */

public class ImageTagSearchActivity extends BaseUIActivity{
    private int mType;

    @Bind(R.id.image_tag_id_title)
    TextView mTvTitle;
    @Bind(R.id.search_id_search)
    EditText mEtSearch;
    @Bind(R.id.search_id_list)
    ListView mLvList;

    private SearchImageTagAdapter mAdapter;
    private ArrayList<Tag> mTags;
    private List<Tag> mDefaultTags;

    @Override
    protected long initOptions() {
        return UIOptions.UI_OPTIONS_SYSTEMBAR
                | UIOptions.UI_OPTIONS_APPBAR_CUSTIOM
                | UIOptions.UI_OPTIONS_CONTENT_CUSTOM;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mType = getIntent().getIntExtra(CstOuer.KEY.TYPE, EImageTagType.IMAGE_TAG_BRAND);
    }

    @Override
    protected void initAppCustom() {
        setAppCustomLayout(R.layout.layout_image_tag_edit_header);
    }
    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_image_tag_search);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        if(mType == EImageTagType.IMAGE_TAG_BRAND) {
            mTvTitle.setText(R.string.image_tag_edit_brand);
            mEtSearch.setHint(R.string.image_tag_edit_brand_search_hint);
        } else if(mType == EImageTagType.IMAGE_TAG_PRICE) {
            mTvTitle.setText(R.string.image_tag_edit_price);
            mEtSearch.setHint(R.string.image_tag_edit_price_search_hint);
        } else if(mType == EImageTagType.IMAGE_TAG_ADDRESS) {
            mTvTitle.setText(R.string.image_tag_edit_address);
            mEtSearch.setHint(R.string.image_tag_edit_address_search_hint);
        }


        //搜索
        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int arg1, KeyEvent keyEvent) {
                if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
                    String key = mEtSearch.getText().toString().trim();
                    searchTag(key);
                }

                return false;
            }
        });


        mEtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String key = mEtSearch.getText().toString().trim();
                if(UtilString.isBlank(key)) {
                    mAdapter.setList(mDefaultTags);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        mAdapter = new SearchImageTagAdapter(this);
        mLvList.setAdapter(mAdapter);


        mLvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String value = mAdapter.getData().get(position).getName();
                OuerDispatcher.sendImageTagEditBroadcast(ImageTagSearchActivity.this, mType, value);
                finish();
            }
        });

        defaultTag();
    }


    /**
     * 默认标签
     */
    private void defaultTag() {
        AgnettyFuture future = OuerApplication.mOuerFuture.imageTags(mType,
                0, 100, "",
                new OuerFutureListener(this) {
                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        mDefaultTags = (List<Tag>) result.getAttach();
                        mAdapter.setList(mDefaultTags);
                    }
                }) ;

        attachDestroyFutures(future);
    }

    /**
    * 搜索标签
    */
    private void searchTag(final String key) {
        AgnettyFuture future = OuerApplication.mOuerFuture.imageTags(
                mType, 0, CstOuer.PAGE.DEFAULT_SIZE, key,
                new OuerFutureListener(this) {
                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        List<Tag> tags = (List<Tag>) result.getAttach();
                        mAdapter.setList(tags);
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);

                    }

                    @Override
                    public void onNetUnavaiable(AgnettyResult result) {

                    }
                }) ;

        attachDestroyFutures(future);
    }


    /**
     * 返回
     */
    @OnClick(R.id.image_tag_id_nav)
    public void goNav() {
        finish();
    }

    /**
     * 完成
     */
    @OnClick(R.id.image_tag_id_finish)
    public void goFinish() {
        String value = mEtSearch.getText().toString().trim();
        if(UtilString.isBlank(value)) {
            if(mType == EImageTagType.IMAGE_TAG_BRAND) {
                UtilOuer.toast(this, R.string.image_tag_edit_brand_search_hint);
            } else if(mType == EImageTagType.IMAGE_TAG_PRICE) {
                UtilOuer.toast(this, R.string.image_tag_edit_price_search_hint);
            } else if(mType == EImageTagType.IMAGE_TAG_ADDRESS) {
                UtilOuer.toast(this, R.string.image_tag_edit_address_search_hint);
            }

            return;
        }

        OuerDispatcher.sendImageTagEditBroadcast(this, mType, value);
        finish();
    }


}
